nodejs: fix accept_waveform error handling and add missing recognizer/model methods - #2056
Open
Dhruvy0804 wants to merge 1 commit into
Open
nodejs: fix accept_waveform error handling and add missing recognizer/model methods#2056Dhruvy0804 wants to merge 1 commit into
Dhruvy0804 wants to merge 1 commit into
Conversation
…t_waveform error handling vosk_recognizer_accept_waveform was declared to ffi as returning 'bool', but the C API returns an int and documents -1 for the error case. ffi coerces -1 to true, so a failure to process a chunk was reported to the caller as "end of utterance" and the next result() call returned stale data instead of surfacing the error. Declare the return as 'int' and raise, like the Python binding does. acceptWaveform still returns a boolean, so existing callers are unaffected. Also bind the configuration calls that were already available in the C, Python, Java and C# bindings but missing here: - Model.findWord() -> vosk_model_find_word - Recognizer.setNlsml() -> vosk_recognizer_set_nlsml - Recognizer.setGrammar() -> vosk_recognizer_set_grm - Recognizer.setEndpointerMode() -> vosk_recognizer_set_endpointer_mode - Recognizer.setEndpointerDelays() -> vosk_recognizer_set_endpointer_delays along with an EndpointerMode enum mirroring VoskEndpointerMode. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two related changes to the Node.js binding.
Fix:
accept_waveformerrors were reported as end-of-utterancevosk_recognizer_accept_waveformis declared to ffi as returning'bool':but the C API returns an
intand documents three states:ffi coerces the non-zero
-1totrue, so whenRecognizer::AcceptWaveformthrows andvosk_api.ccreturns-1, the Node caller sees exactly what it sees on a successfulendpoint. Every demo follows the
if (rec.acceptWaveform(data)) ... rec.result()shape,so the failure surfaces as a stale/empty result rather than an error.
This declares the return as
'int'and throws on a negative value, matching what thePython binding already does:
acceptWaveformandacceptWaveformAsyncstill resolve to a boolean, so existingcallers and all of the
demo/scripts are unaffected on the success path.Add the configuration calls that were already exported
These are in
vosk_api.hand reachable from the C, Python, Java and C# bindings, butwere not bound here — the README notes that "some methods are not yet fully implemented".
Model.findWord()vosk_model_find_wordRecognizer.setNlsml()vosk_recognizer_set_nlsmlRecognizer.setGrammar()vosk_recognizer_set_grmRecognizer.setEndpointerMode()vosk_recognizer_set_endpointer_modeRecognizer.setEndpointerDelays()vosk_recognizer_set_endpointer_delaysAlso exports an
EndpointerModeenum mirroringVoskEndpointerMode, named as in thePython binding:
setGrammartakes an array and stringifies it, consistent with how thegrammarconstructor option is already handled;
[]switches back to the default model graph.All new symbols already ship in the 0.3.75 libraries that this package version pairs
with, so
ffi.Libraryresolution is unaffected.Notably left out: the batch/GPU calls (need a CUDA build to exercise) and the text
processor calls (
vosk_text_processor_itnreturns astrduped buffer with nocorresponding free in the C API, so binding it would leak).
JSDoc is kept in the existing style and the file still passes
tsc --checkJswith nonew diagnostics.